package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_Submit_ZhanhuiActivity extends Activity {
    //    private SharedPreferences sp;
//    private Editor ed;
    private EditText edittext_zhanhui_content;
    private TextView text_zhanhui_submit;
    private RelativeLayout relative_submit_zhanhui_back;
    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__submit__zhanhui);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        pd = new ProgressDialog(this);

        edittext_zhanhui_content = (EditText) findViewById(R.id.edittext_zhanhui_content);
        text_zhanhui_submit = (TextView) findViewById(R.id.text_zhanhui_submit);
        relative_submit_zhanhui_back = (RelativeLayout) findViewById(R.id.relative_submit_zhanhui_back);
        text_zhanhui_submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pd.show();
                zhanhui_submit_data();
            }
        });

        relative_submit_zhanhui_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_Submit_ZhanhuiActivity.this.finish();
            }
        });
    }

    public void zhanhui_submit_data() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();


        parms.addBodyParameter("zhanhui_id", getIntent().getIntExtra("zhanhui_id", 0) + "");

        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("details", edittext_zhanhui_content.getText().toString());
        httpUtils.send(HttpMethod.POST, Contant.zhanhui_details, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    Toast.makeText(Main_Submit_ZhanhuiActivity.this, object.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (object.getInt("status") == 1) {
                        pd.cancel();
                        Main_Submit_ZhanhuiActivity.this.finish();
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }
}